package com.filex.ads.interstitial;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;

import com.filex.ads.common.BaseBroadcastReceiver;
import com.filex.ads.common.IntentActions;


public class IntersEventBroadcastReceiver extends BaseBroadcastReceiver {

    private final InternalAdListener mInternalAdListener;
    private static IntentFilter sIntentFilter;

    public IntersEventBroadcastReceiver(InternalAdListener internalAdListener, final long eventId) {
        super(eventId);
        mInternalAdListener = internalAdListener;
        getIntentFilter();
    }

    @NonNull
    public IntentFilter getIntentFilter() {
        if (sIntentFilter == null) {
            sIntentFilter = new IntentFilter();
            sIntentFilter.addAction(IntentActions.ACTION_INTERSTITIAL_FAIL);
            sIntentFilter.addAction(IntentActions.ACTION_INTERSTITIAL_SHOW);
            sIntentFilter.addAction(IntentActions.ACTION_INTERSTITIAL_DISMISS);
            sIntentFilter.addAction(IntentActions.ACTION_INTERSTITIAL_CLICK);
        }
        return sIntentFilter;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (mInternalAdListener == null) {
            return;
        }

        if (!shouldConsumeBroadcast(intent)) {
            return;
        }

        final String action = intent.getAction();
        if (IntentActions.ACTION_INTERSTITIAL_FAIL.equals(action)) {
            mInternalAdListener.onInterstitialFailed();
        } else if (IntentActions.ACTION_INTERSTITIAL_SHOW.equals(action)) {
            mInternalAdListener.onInterstitialShow();
        } else if (IntentActions.ACTION_INTERSTITIAL_DISMISS.equals(action)) {
            mInternalAdListener.onInterstitialDismissed();
            unregister(this);
        } else if (IntentActions.ACTION_INTERSTITIAL_CLICK.equals(action)) {
            mInternalAdListener.onInterstitialClicked();
        }
    }

    interface InternalAdListener {
        void onInterstitialFailed();

        void onInterstitialShow();

        void onInterstitialDismissed();

        void onInterstitialClicked();
    }
}
